package com.ffcs.cn.action.desktopcontrol.smButtonColorSelector;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.colorSelect.ColorSelectionPopupMenu;
import com.supermap.desktop.controls.ui.controls.colorSelect.SmButtonColorSelector;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;


public class DialogSmButtonColorSelector extends SmDialog {
	private SmButtonColorSelector buttonColorSelector;

	private JCheckBox checkBoxAlpha;
	private ColorSelectionPopupMenu selectionPopupMenu;
	private JButton button;

	public DialogSmButtonColorSelector() {
		setTitle("SmButtonColorSelector 示例");
		setSize(new Dimension(330,170));
		initComponent();
		initLayout();
		initListener();
	}

	private void initListener() {
		//设置颜色弹出菜单
		button.addActionListener(e -> selectionPopupMenu.show(button,0,button.getHeight()));
		//开启 alpha
		checkBoxAlpha.addActionListener(e -> buttonColorSelector.setAlphaEnabled(checkBoxAlpha.isSelected()));
		selectionPopupMenu.addColorChangedListener(colorChangedEvent -> {
			//设置颜色
			buttonColorSelector.setColor(colorChangedEvent.getColor());
		});

	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private JPanel getRightPanel() {
		JPanel right = new JPanel();
		right.setLayout(new GridBagLayout());
		right.setBorder(new TitledBorder("控制面板"));
		right.add(checkBoxAlpha, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.HORIZONTAL));

		right.add(button, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.HORIZONTAL));
		return right;
	}

	private JPanel getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控件"));
		panel.add(buttonColorSelector, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL));
		return panel;
	}

	private void initComponent() {
		buttonColorSelector = new SmButtonColorSelector();

		checkBoxAlpha = new JCheckBox("开启 Alpha");
		checkBoxAlpha.setSelected(buttonColorSelector.isAlphaEnabled());

		button = new JButton("设置颜色");
		selectionPopupMenu = new ColorSelectionPopupMenu();
	}


}
